﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ReadSolutionContents
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void opendialog_Click(object sender, EventArgs e)
        {
            if (allcsfiles.Checked || alldirectory.Checked)
            {
                if (folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    SelectePathProject.Text = folderBrowserDialog1.SelectedPath;
                }

            }
            else if (singlefile.Checked)
            {
                if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    SelectePathProject.Text = openFileDialog1.FileName;
                }
            }
            else
            {
                MessageBox.Show("Please Select a option Specified", "StyleCop CMD", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
            }
        }

        private Scanner GetScanObject()
        {
            Scanner scan = new Scanner();

            if (!string.IsNullOrEmpty(SettingsFile.Text))
            {
                if (!System.IO.File.Exists(SettingsFile.Text))
                {
                    throw new System.IO.FileNotFoundException(SettingsFile.Text);
                }

                scan.SettingsFile = SettingsFile.Text;
            }

            if (!string.IsNullOrEmpty(Outputfile.Text))
            {
                if (!System.IO.File.Exists(Outputfile.Text))
                {
                    throw new System.IO.FileNotFoundException(Outputfile.Text);
                }

                scan.ViolationFile = Outputfile.Text;
            }
            scan.Info += InfoEvent;
            return scan;
        }

        private void StartScan_Click(object sender, EventArgs e)
        {
            SolutionReader reader = new SolutionReader();

            Scanner scan = GetScanObject();

            string[] files;
            if (!string.IsNullOrEmpty(SelectePathProject.Text))
            {
                if (allcsfiles.Checked || alldirectory.Checked)
                {
                    if (!System.IO.Directory.Exists(SelectePathProject.Text))
                    {
                        throw new System.IO.DirectoryNotFoundException(SelectePathProject.Text);
                    }

                    if (allcsfiles.Checked)
                    {
                        files = reader.GetAllCSharpFiles(SelectePathProject.Text);
                        scan.Filelist = files;
                        scan.Location = SelectePathProject.Text;
                        scan.ScanProject();
                    }

                    if (alldirectory.Checked)
                    {
                        string[] allsolutions;
                        allsolutions = reader.GetEachSolution(SelectePathProject.Text);


                        string[][] multiplefilelist = new string[allsolutions.Length][];

                        for (int i = 0; i < allsolutions.Count(); i++)
                        {
                            string[] filearray = reader.GetFilesForProject(allsolutions[i]);
                            multiplefilelist[i] = filearray;

                           
                            //scan.ScanProject();
                        }
                        scan.ScanMultipleProjects(allsolutions, multiplefilelist);

                        //foreach (var solution in allsolutions)
                        //{
                        //    string[] filearray = reader.GetFilesForProject(solution);
                        //    scan.Filelist = filearray;

                        //    System.IO.FileInfo p = new System.IO.FileInfo(solution);
                        //    scan.Location = p.DirectoryName;
                        //    scan.ScanProject();
                        //}
                    }
                }
                else if (singlefile.Checked)
                {
                    string[] filetoscan;
                    if (SelectePathProject.Text.EndsWith(".sln"))
                    {
                        filetoscan = reader.GetFilesForProject(SelectePathProject.Text);
                    }
                    else if (SelectePathProject.Text.EndsWith(".cs"))
                    {
                        filetoscan = new string[] { SelectePathProject.Text };
                    }
                    else
                    {
                        return;
                    }

                    scan.Filelist = filetoscan;
                    System.IO.FileInfo p = new System.IO.FileInfo(SelectePathProject.Text);
                    scan.Location = p.DirectoryName;
                    scan.ScanProject();
                }
            }
        }

        private void selectSettingsfile_Click(object sender, EventArgs e)
        {
            var filedialog = new OpenFileDialog();
            filedialog.Filter = "Settings File |*.StyleCop";

            if (filedialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                SettingsFile.Text = filedialog.FileName;
            }
            filedialog.Dispose();
        }

        private void selectoutputfile_Click(object sender, EventArgs e)
        {
            var filedialog = new OpenFileDialog();
            filedialog.Filter = "Settings File |*.xml";

            if (filedialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Outputfile.Text = filedialog.FileName;
            }

            filedialog.Dispose();
        }


        void InfoEvent(object sender, string message)
        {
            outputtextbox.AppendText("\r\n" + message);
        }
    }
}
